<!-- 个人中心界面 -->
<template>
  <div id="login">
    <div class="bg"></div>
    <el-row class="main-container">
      <el-col :lg="8" :xs="16" :md="10" :span="10">
        <div class="top">
          <i class="iconfont icon-kaoshi"></i><span class="title">个人中心</span>
        </div>
        <div class="bottom">
          <div class="container">
            <p class="title">修改个人信息</p>
            <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign" :rules="rules" ref="ruleForm">
              <el-form-item label="用户名" prop="username">
                <el-input v-model="formLabelAlign.username" placeholder="请输入用户名"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input v-model="formLabelAlign.password" placeholder="请输入密码" type='password'></el-input>
              </el-form-item>
              <el-form-item label="重复密码" prop="rpassword">
                <el-input v-model="rpassword" placeholder="请重复输入密码" type='password'></el-input>
              </el-form-item>
              <el-form-item label="电话" prop="phone">
                <el-input v-model="formLabelAlign.phone" placeholder="请输入电话"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="formLabelAlign.email" placeholder="请输入邮箱"></el-input>
              </el-form-item>
              <div class="submit">
                <el-button type="primary" class="row-login" @click="submit('ruleForm')">修改</el-button>
              </div>
            </el-form>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row class="footer">
      <el-col>
        <p class="msg2">版权所有 ©2022 <!--智鹏有限公司--> 保留所有权利  <a href="http://beian.miit.gov.cn/" target="_blank">刘ICP备20204123号</a></p>
      </el-col>
    </el-row>
    <section class="remind" v-on:click="goBack">
      <span style="text-align:center" >返回首页</span>
    </section>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: "login",
  data() {
    return {
      role: 2,
      labelPosition: 'left',
      rpassword : null,
      formLabelAlign: {
        username: '',
        password: '',
        status: null ,
        phone: '',
        email:''
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3 , max: 20, message: "长度在 3 到 20 个字符" ,trigger:["blur", "submit"]}
        ],
        // 包含小数的数字验证
        password: [
          {
            validator: (rule, value, callback) => {
              let reg = /^[a-zA-Z0-9]{6,15}$/;
              if (!value) {
                return callback(new Error("密码不能为空"));
              }
              setTimeout(() => {
                if (!reg.test(value)) {
                  callback(new Error("密码由长度6-15位数字和字母组成"));
                } else {
                    callback();
                  }
                
              }, 1000);
            },
            trigger: ["blur", "submit"],
            required: true
          }
        ],
        rpassword: [
          {
            validator: (rule, value, callback) => {
              value = this.rpassword
              //包含小数的数字
              if (!value) {
                return callback(new Error("密码不能为空"));
              }
              setTimeout(() => {
                if (value != this.formLabelAlign.password) {
                  callback(new Error("两次输入密码不一致"));
                } else {
                    callback();
                  }
                
              }, 1000);
            },
            trigger: ["blur", "submit"],
            required: true
          }
        ],
        phone: [
          { required: true, trigger: ["blur", "submit"] ,
            validator: (rule, value, callback) => {
                let reg = /^0?(13|14|15|18|17)[0-9]{9}$/;
                if (!value) {
                  return callback(new Error("请输入手机号"));
                }
                setTimeout(() => {
                  if (!reg.test(value)) {
                    callback(new Error("手机号长度或格式不正确"));
                  }  else {
                      callback();
                    }
                  
                }, 1000);
              }},
        ],
        email: [
          { required: true, trigger: ["blur", "submit"], 
            validator: (rule, value, callback) => {
              //包含小数的数字
              let reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
              if (!value) {
                return callback(new Error("请输入邮箱"));
              }
              setTimeout(() => {
                if (!reg.test(value)) {
                  callback(new Error("请输入正确格式的邮箱"));
                  } else {
                    callback();
                  }
              }, 1000);
            },
          }],
        status:[
          { required: true, message: "请输入身份", trigger: ["blur", "submit"] }        
        ]
      }
    }
  },
  created(){
  this.getInfo(this.$cookies.get("cname")) 
  },

  methods: {
	  goBack(){
	          if (window.history.length <= 1) {
	              this.$router.push({path:'/'})
	              return false
	          } else {
	              this.$router.go(-1)
	          }
	  },
	  submit(){
	  	this.$axios({
	  	  url: '/api/student',
	  	  method: 'put',
	  	  data: {
	  	    ...this.form
	  	  }
	  	}).then(res => {
	  	  console.log(res)
	  	  if(res.data.code ==200) {
	  	    this.$message({
	  	      message: '更新成功',
	  	      type: 'success'
	  	    })
	  	  }
	  	this.getInfo(this.$cookies.get("cname"))  
	  	})
	  },
	getInfo(username){
		this.$axios(`/api/user/${username}`).then(res => {
		  this.formLabelAlign = res.data.data;
		})
	},
 
    // 导入测试数据
    testData(){
      var formData = {
        username: 'admin',
        password: '123456',
        status: null ,
        phone: '13685015577',
        email:'971324219@qq.com'
      };
      this.formLabelAlign = formData;
      this.rpassword = '123456';
    },



    // //用户注册
    // register(formName) {
    //   this.$refs[formName].validate((valid) => {
    //     if(valid){
    //       console.log("注册操作执行-------");
    //       this.$axios({
    //         url: `/api/register`,
    //         method: 'post',
    //         data: {
    //           ...this.formLabelAlign
    //         }
    //       }).then(res=>{
    //         let resData = res.data.data
    //         if(resData != null) {

    //           console.log(resData)

    //         }
    //         if(resData == null) { //错误提示
    //           this.$message({
    //             showClose: true,
    //             type: 'error',
    //             message: '用户名已存在，请重新输入'
    //           })
    //         }
    //       })
    //     } else{
    //       this.$message({
    //             showClose: false,
    //             type: 'error',
    //             message: '请按照规范注册账户'
    //           })
    //       return false
    //     }
    //   })
      
        


    // },
    clickTag(key) {
      this.role = key
    }
  },
  computed: mapState(["userInfo"]),
  mounted() {

  }
}
</script>

<style lang="less" scoped>
.remind {
  border-radius: 4px;
  padding: 10px 20px;
  display: flex;
  position: fixed;
  right: 20px;
  bottom: 50%;
  flex-direction: column;
  color: #606266;
  background-color: #fff;
  border: 4px solid #409eff;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
.container {
  margin-bottom: 32px;
}
.container .el-radio-group {
  margin: 3000px 0px;
}
a:link {
  color:#ff962a;
  text-decoration:none;
}
#login {
  font-size: 14px;
  color: #000;
  background-color: #fff;
}
#login .bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  overflow-y: auto;
  height: 100%;
  background: url('../../assets/img/loginbg.png')center top / cover no-repeat;
  background-color: #b6bccdd1 !important;
}
#login .main-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
#login .main-container .top {
  margin-top: 1px;
  font-size: 30px;
  color: #ff962a;
  display: flex;
  justify-content: center;
}
#login .top .icon-kaoshi {
  font-size: 80px;
}
#login .top .title {
  margin-top: 20px;
}
#login .bottom {
  display:flex;
  justify-content: center;
  background-color:#fff;
  border-radius: 5px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
#login .bottom .title {
  text-align: center;
  font-size: 30px;
}
#back {
  position: absolute;
  font-size: 35px;
  left: 520px;
  top: 200px;
 
}
.bottom .container .title {
  margin: 30px 0px;;
}
.bottom .submit .row-login {
  width: 100%;
  background-color: #04468b;
  border-color: #04468b;
  margin: 20px 0px 10px 0px;
  padding: 15px 20px;
}
.bottom .submit {
  display: flex;
  justify-content: center;
}
.footer {
  margin-top: 50px;
  text-align: center;
}
.footer .msg1 {
  font-size: 18px;
  color: #fff;
  margin-bottom: 15px;
}
.footer .msg2 {
  font-size: 14px;
  color: #e3e3e3;
  margin-top: 70px;
}
.bottom .options {
  margin-bottom: 40px;
  color: #ff962a;
  display: flex;
  justify-content: space-between;
}
.bottom .options > a {
  color: #ff962a;
}
.bottom .options .register span:nth-child(1) {
  color: #8C8C8C;
}
</style>
